package com.smartlink.app.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.smartlink.app.util.DatabaseHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 保存下载的教室信息
 * Created by Administrator on 2016/7/11.
 */
public class ClassRoomInfoDao {
    private static final String TAG = "ClassRoomInfoDao";
    private Context ctx;
    private DatabaseHelper databaseHelper;

    public ClassRoomInfoDao(Context ctx) {
        this.ctx = ctx;
        databaseHelper = new DatabaseHelper(ctx);
    }

    /**
     * 保存教室信息
     *
     * @param list
     */
    public void insertClassRoomInfo(List<ArrayList> list) {
        SQLiteDatabase database = databaseHelper.getWritableDatabase();
        String sql = "INSERT INTO class_room_info(class_room_id,bluetooth,class_room_name) VALUES(?,?,?)";
        database.beginTransaction();
        SQLiteStatement statement = database.compileStatement(sql);
        int size = list.size();
        for (int i = 0; i < size; i++) {
            ArrayList objects = list.get(i);
            statement.bindString(1, objects.get(0).toString());
            statement.bindString(2, objects.get(1).toString());
            statement.bindString(3, objects.get(2).toString());
            statement.execute();
            statement.clearBindings();
        }
        database.setTransactionSuccessful();
        database.endTransaction();
        database.close();
        Log.i(TAG, "insertClassRoomInfo: SAVE SUCCESS>>>>>>>>>>>>>>>>>>>");
    }

    /**
     * 查询教室信息
     *
     * @param bluetooth
     * @return
     */
    public String[] findClassRoomInfoByBluetooth(String bluetooth) {
        SQLiteDatabase database = databaseHelper.getReadableDatabase();
        String sql = "SELECT class_room_id,class_room_name FROM class_room_info WHERE bluetooth=? ";
        Cursor cursor = database.rawQuery(sql, new String[]{bluetooth});
        String[] infos = new String[3];
        while (cursor.moveToNext()) {
            infos[0] = cursor.getString(cursor.getColumnIndexOrThrow("class_room_id"));
            infos[1] = cursor.getString(cursor.getColumnIndexOrThrow("class_room_name"));
            infos[2]=bluetooth;
        }
        cursor.close();
        database.close();
        return infos;
    }

    public void deleteInfos(){
        SQLiteDatabase database = databaseHelper.getWritableDatabase();
        String sql="DELETE FROM class_room_info";
        database.execSQL(sql);
        database.close();
    }


}
